package net.stogi.unicore.beyond.ui.server.taskeditor.action.handler;

import net.stogi.common.api.exception.BusinessException;
import net.stogi.common.gwt.dispatch.server.core.ActionHandler;
import net.stogi.common.gwt.dispatch.shared.exception.ActionException;
import net.stogi.unicore.beyond.api.service.TaskService;
import net.stogi.unicore.beyond.ui.shared.taskeditor.action.model.SaveTaskDefinitionAction;
import net.stogi.unicore.beyond.ui.shared.taskeditor.action.model.SaveTaskDefinitionResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("UnicoreBeyondUi.Dispatch.SaveTaskDefinitionActionHandler")
public class SaveTaskDefinitionActionHandler implements ActionHandler<SaveTaskDefinitionAction, SaveTaskDefinitionResult> {

	@Autowired
	private TaskService taskService;
	
	@Override
	public SaveTaskDefinitionResult execute(SaveTaskDefinitionAction action) throws ActionException {
		try {
			taskService.saveTaskDefinition(action.getTaskDefinition());
			return new SaveTaskDefinitionResult();
		}
		catch (BusinessException e) {
			throw new ActionException(e);
		}
	}

	@Override
	public Class<SaveTaskDefinitionAction> getActionType() {
		return SaveTaskDefinitionAction.class;
	}

	@Override
	public void rollback(SaveTaskDefinitionAction action, SaveTaskDefinitionResult result) throws ActionException {}
}
